﻿namespace CKFinder.Connector.CommandHandlers
{
    using CKFinder;
    using CKFinder.Connector;
    using System;
    using System.IO;
    using System.Security;
    using System.Xml;

    internal class RenameFileCommandHandler : XmlCommandHandlerBase
    {
        protected override void BuildXml()
        {
            if (!base.CurrentFolder.CheckAcl(AccessControlRules.FileRename))
            {
                ConnectorException.Throw(0x67);
            }
            string attributeValue = base.Request["fileName"];
            string fileName = base.Request["newFileName"];
            XmlNode node = XmlUtil.AppendElement(base.ConnectorNode, "RenamedFile");
            XmlUtil.SetAttribute(node, "name", attributeValue);
            if (!CKFinder.Connector.Connector.CheckFileName(attributeValue) || Config.Current.CheckIsHiddenFile(attributeValue))
            {
                ConnectorException.Throw(0x6d);
            }
            else if (!base.CurrentFolder.ResourceTypeInfo.CheckExtension(Path.GetExtension(attributeValue)))
            {
                ConnectorException.Throw(0x6d);
            }
            else if (!CKFinder.Connector.Connector.CheckFileName(fileName) || Config.Current.CheckIsHiddenFile(fileName))
            {
                ConnectorException.Throw(0x66);
            }
            else
            {
                if (!base.CurrentFolder.ResourceTypeInfo.CheckExtension(Path.GetExtension(fileName)))
                {
                    ConnectorException.Throw(0x69);
                }
                string path = Path.Combine(base.CurrentFolder.ServerPath, attributeValue);
                string str4 = Path.Combine(base.CurrentFolder.ServerPath, fileName);
                bool flag = false;
                if (!File.Exists(path))
                {
                    ConnectorException.Throw(0x75);
                }
                if (File.Exists(str4) || Directory.Exists(str4))
                {
                    ConnectorException.Throw(0x73);
                }
                try
                {
                    File.Move(path, str4);
                    flag = true;
                    XmlUtil.SetAttribute(node, "newName", fileName);
                }
                catch (UnauthorizedAccessException)
                {
                    ConnectorException.Throw(0x68);
                }
                catch (SecurityException)
                {
                    ConnectorException.Throw(0x68);
                }
                catch (ArgumentException)
                {
                    ConnectorException.Throw(0x66);
                }
                catch (NotSupportedException)
                {
                    ConnectorException.Throw(0x66);
                }
                catch (PathTooLongException)
                {
                    ConnectorException.Throw(0x66);
                }
                catch (IOException)
                {
                    ConnectorException.Throw(110);
                }
                catch (Exception)
                {
                    ConnectorException.Throw(110);
                }
                if (flag)
                {
                    try
                    {
                        File.Delete(Path.Combine(base.CurrentFolder.ThumbsServerPath, attributeValue));
                    }
                    catch
                    {
                    }
                }
            }
        }
    }
}

